﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DeviceLib;

namespace DeviceLib
{
    /// <summary>
    /// BaseControl.xaml 的交互逻辑
    /// </summary>
    public class BaseControl : ContentControl, Interfaces.IDevice
    {
        public delegate void DeviceMoved(ContentControl sender, Point mousePosition);
        public event DeviceMoved OnDeviceMoved;
        protected Control basePlateform;
        protected Point baseSize;
        public bool isSelected { set; get; }
        public BaseControl()
        {
        }

        public double X
        {
            set { SetValue(XProperty, value); }
            get { return (double)GetValue(XProperty); }
        }
        public double Y
        {
            set { SetValue(YProperty, value); }
            get { return (double)GetValue(YProperty); }
        }

        private Point _currentPoint;
        private Point _startPoint;
        

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                _currentPoint = e.GetPosition(this);
                X += _currentPoint.X - _startPoint.X;
                Y += _currentPoint.Y - _startPoint.Y;
                OnDeviceMoved(this, new Point(X, Y));
            }
        }

        protected override void OnMouseDown(MouseButtonEventArgs e)
        {
            base.OnMouseDown(e);
            _startPoint = e.GetPosition(this);
            this.CaptureMouse();
        }

        protected override void OnMouseUp(MouseButtonEventArgs e)
        {
            base.OnMouseUp(e);
            this.ReleaseMouseCapture();
        }
        public virtual byte[] GetSuperviseGram()
        {
            List<byte> gram = new List<byte>();
            return gram.ToArray();
        }

        public string GetDeviceName()
        {
            throw new NotImplementedException();
        }

        public void AnalysisGram(byte[] gram)
        {
            throw new NotImplementedException();
        }

        public byte[] GetConfigAddressGram(int newAddr)
        {
            throw new NotImplementedException();
        }

        public void SetDeviceLocation(double positionX, double positionY)
        {
            throw new NotImplementedException();
        }

        public static readonly DependencyProperty XProperty =
            DependencyProperty.Register("X", typeof(double), typeof(BaseControl), new PropertyMetadata((double)0));

        public static readonly DependencyProperty YProperty =
            DependencyProperty.Register("Y", typeof(double), typeof(BaseControl), new PropertyMetadata((double)0));
    }
}
